package com.ruoyi.products.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.products.domain.Video;
import com.ruoyi.products.service.IVideoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.domain.entity.SysUser;

/**
 * 选品视频Controller
 *
 * @author star
 * @date 2025-06-04
 */
@RestController
@RequestMapping("/products/video")
public class VideoController extends BaseController
{
    @Autowired
    private IVideoService videoService;

    /**
     * 导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Video> util = new ExcelUtil<Video>(Video.class);
        util.importTemplateExcel(response, "商品数据");
    }

    @Log(title = "商品导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Video> util = new ExcelUtil<Video>(Video.class);
        List<Video> userList = util.importExcel(file.getInputStream());

        for (Video video : userList) {
            videoService.insertVideo(video);
        }

        //todo 把解析的数据 导入数据库
        return success("成功");
    }

    /**
     * 查询选品视频列表
     * 该接口不可 增 微信小程序 当前使用
     * 如果有其他逻辑 自行添加接口
     */
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(Video video)
    {
        startPage();
        List<Video> list = videoService.selectVideoList(video);
        return getDataTable(list);
    }

    /**
     * 导出选品视频列表
     */
    @PreAuthorize("@ss.hasPermi('products:video:export')")
    @Log(title = "选品视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Video video)
    {
        List<Video> list = videoService.selectVideoList(video);
        ExcelUtil<Video> util = new ExcelUtil<Video>(Video.class);
        util.exportExcel(response, list, "选品视频数据");
    }

    /**
     * 获取选品视频详细信息
     */
    @PreAuthorize("@ss.hasPermi('products:video:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(videoService.selectVideoById(id));
    }

    /**
     * 新增选品视频
     */
    @PreAuthorize("@ss.hasPermi('products:video:add')")
    @Log(title = "选品视频", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Video video)
    {
        return toAjax(videoService.insertVideo(video));
    }

    /**
     * 修改选品视频
     */
    @PreAuthorize("@ss.hasPermi('products:video:edit')")
    @Log(title = "选品视频", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Video video)
    {
        return toAjax(videoService.updateVideo(video));
    }

    /**
     * 删除选品视频
     */
    @PreAuthorize("@ss.hasPermi('products:video:remove')")
    @Log(title = "选品视频", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(videoService.deleteVideoByIds(ids));
    }
}
